package com.ruoyi.supplier.controller;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.ruoyi.supplier.domain.TbProduct;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supplier.domain.TbPrice;
import com.ruoyi.supplier.service.ITbPriceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 价格Controller
 * 
 * @author ruoyi
 * @date 2021-02-24
 */
@RestController
@RequestMapping("/supplier/price")
public class TbPriceController extends BaseController
{
    @Autowired
    private ITbPriceService tbPriceService;

    /**
     * 查询价格列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('supplier:price:list')")
    public TableDataInfo list(TbPrice tbPrice)
    {
        startPage();
        List<TbPrice> list = tbPriceService.selectTbPriceList(tbPrice);
        AtomicInteger key = new AtomicInteger();
        list.forEach(tp -> {
            key.getAndIncrement();
            tp.setAutoId(key.get());
        });

        return getDataTable(list);
    }

    /**
     * 查询价格列表
     */
    @GetMapping("/listProduct")
    @PreAuthorize("@ss.hasPermi('supplier:price:list')")
    public TableDataInfo listProduct(TbPrice tbPrice)
    {
        startPage();
        List<TbPrice> list = tbPriceService.selectTbPriceListProduct(tbPrice);
        return getDataTable(list);
    }

    /**
     * 导出价格列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:price:export')")
    @Log(title = "价格", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbPrice tbPrice)
    {
        List<TbPrice> list = tbPriceService.selectTbPriceList(tbPrice);
        ExcelUtil<TbPrice> util = new ExcelUtil<TbPrice>(TbPrice.class);
        return util.exportExcel(list, "price");
    }

    /**
     * 获取价格详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplier:price:query')")
    @GetMapping(value = "/{priceId}")
    public AjaxResult getInfo(@PathVariable("priceId") Long priceId)
    {
        
        return AjaxResult.success(tbPriceService.selectTbPriceById(priceId));
    }

    /**
     * 新增价格
     */
    @PreAuthorize("@ss.hasPermi('supplier:price:add')")
    @Log(title = "价格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbPrice tbPrice)
    {
        return toAjax(tbPriceService.insertTbPrice(tbPrice));
    }

    /**
     * 修改价格
     */
    @PreAuthorize("@ss.hasPermi('supplier:price:edit')")
    @Log(title = "价格", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbPrice tbPrice)
    {
        return toAjax(tbPriceService.updateTbPrice(tbPrice));
    }

    /**
     * 修改价格
     */
    @Log(title = "价格", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/batch")
    public AjaxResult editBatch(@RequestBody List<TbPrice> tbPrice)
    {
        return toAjax(tbPriceService.updateTbPrice(tbPrice));
    }

    /**
     * 修改价格
     */
    @Log(title = "价格", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/set")
    public AjaxResult editSet(@RequestBody TbProduct tbProduct)
    {
        int i = tbPriceService.updateTbPriceSet(tbProduct);
        if(i==0){
            return AjaxResult.error("已经是最新的商品规格!");
        }
        return  AjaxResult.success("更新成功",i);
    }

    /**
     * 删除价格
     */
    @PreAuthorize("@ss.hasPermi('supplier:price:remove')")
    @Log(title = "价格", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbPriceService.deleteTbPriceByIds(ids));
    }
}
